package com.trkj.service;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.LmbConsumptiveStockDAO;
import com.trkj.dao.jpa.LmbMaterialDamageDAO;
import com.trkj.dao.jpa.LmbMaterialDamageDetailDAO;
import com.trkj.pojos.LmbConsumptiveStock;
import com.trkj.pojos.LmbMaterialDamage;
import com.trkj.pojos.LmbMaterialDamageDetail;
import com.trkj.vo.LmbHcsyVo;

@Service
@Transactional
public class LmbHcsyService {
	@Autowired
	private LmbConsumptiveStockDAO stockDAO;
	@Autowired
	private LmbMaterialDamageDAO damageDAO;
	@Autowired
	private LmbMaterialDamageDetailDAO damageDetailDAO;
	@Autowired
	private LmbOddNumbersService oddNumbersService;
	
	public void pdHcBs(LmbHcsyVo hcsyVo){
		String date = oddNumbersService.rq();
		//拼接
		String name = "HS-" + date;					
		String n = damageDAO.findMaxID(name);
		System.out.println("从数据库中读取的最大损溢编号："+n);
		String sid =oddNumbersService.getId(name, n);
		LmbMaterialDamage damage = hcsyVo.getDamage();
		damage.setMdId(sid);
		damageDAO.save(damage);
		List<LmbMaterialDamageDetail> list = hcsyVo.getDamageDetail();
		if(list !=null){
			for(LmbMaterialDamageDetail damageDetail:list){
				String name1 = "HSX-" + date;	
				String n1 = damageDetailDAO.findMaxID(name1);
				String sid1 = oddNumbersService.getId(name1, n1);
				damageDetail.setMdtId(sid1);
				damageDetail.setMdtMdid(sid);
				damageDetailDAO.save(damageDetail);
				//修改库存数量
				String cid = damageDetail.getMdtCid();
				String batchNum = damageDetail.getMdtBatchNum();
				LmbConsumptiveStock stock = stockDAO.findConStock(cid, batchNum);
				//获取的库存数量
				BigDecimal num = stock.getCstocNum();
				//获取的提领数量
				BigDecimal num1 = damageDetail.getMdtNum();
				//提领之后的库存数量
				BigDecimal cknum = num.subtract(num1);
				stock.setCstocNum(cknum);
				stockDAO.save(stock);
			}
		}
	}
	
}
